from DailyBase import *
from AGDefines import *
import logging

logger = logging.getLogger(__name__)

#通缉悬赏

class TongJi(DailyBase):


    def __init__(self, enum):
        self.RCName = __name__
        self.LogHeader = self.RCName +str(enum.port)
        self.enum = enum
        self.started = False
        self.loadPrivateActions(self.areaDict, TongJiActions)
        logger.info("{} has {} actions".format(self.LogHeader, len(self.areaDict)))

        self.areaDict['RenWuInList'].image = getQueueTitle(self.areaDict['RenWuInList'].image)

        self.rects = []

        sx0 = 57
        sy0 = 41
        width = 104
        height = 153
        w_space = 12
        quchux = 10
        quchuy = 20
        for i in range(5):
            x = sx0 + (width + w_space) * i
            y = sy0
            self.rects.append((x + quchux, y + quchuy, x + width - quchux, y + height - quchuy))

        sx0 += width + w_space
        sy0  = 217
        for i in range(5):
            x = sx0 + (width + w_space) * i
            y = sy0
            self.rects.append((x + quchux, y + quchuy, x + width - quchux, y + height - quchuy))

        self.len = len(self.rects)





    def test(self, img):
        for rect in self.rects:
            cv2.rectangle(img, (rect[0],rect[1]), (rect[2],rect[3]), (0,0,255))
            img0 = img[rect[1]:rect[3], rect[0]:rect[2]]
            score = checkIsGray(img0)
            logger.info("score: " + str(score))

        cv2.imshow("img", img)

        idx = self.findKeJieRenWu(img)
        logger.info("target idx: " + str(idx))
        cv2.waitKey()

    def KaiShi(self):
        self.begin()

    def process_private(self, img=None, data=None):

        if self.areaDict['LingQuTongJi'].detect(img, self.enum) < 10:
            self.enum.click(self.areaDict['LingQuTongJi'].click)
        elif self.areaDict['QianWang'].detect(img, self.enum) < 10:
            self.enum.click(self.areaDict['QianWang'].click, 1000)
        elif self.areaDict['ShouSi'].detect(img, self.enum) < 10:
            self.enum.click(self.areaDict['ShouSi'].click)
        elif self.areaDict['WeiWanCheng'].detect(img, self.enum) < 20 or self.areaDict['YiWanCheng'].detect(img, self.enum) < 20:
            idx = self.findKeJieRenWu(img)
            if idx is STATUS_NOTFOUND:
                return
            rect = self.rects[idx]
            self.enum.click((int((rect[0]+rect[2])/2), int((rect[1]+rect[3])/2), 'Click'+str(idx)))
        else:
            self.findRenWuInList(img)


    def findRenWuInList(self, img):

        serachRect = self.areaDict['RenWuInList'].serachRect

        img[serachRect[1]:serachRect[3], serachRect[0]:serachRect[2]] = getQueueTitle(img[serachRect[1]:serachRect[3], serachRect[0]:serachRect[2]])

        cv2.imshow('img_c',img)

        if self.areaDict['RenWuInList'].detect(img, self.enum) < 20:
            self.enum.click(self.areaDict['RenWuInList'].click)



    def findKeJieRenWu(self, img):
        for i in range(self.len):
            rect = self.rects[self.len - i - 1]
            img0 = img[rect[1]:rect[3], rect[0]:rect[2]]
            score = checkIsGray(img0)
            logger.info("score: " + str(score))
            if(score > 100):
                return self.len - i - 1

        return STATUS_NOTFOUND






